package vm

const (
	MAXARG_Bx  = 1<<18 - 1
	MAXARG_sBx = MAXARG_Bx >> 1
)

// 指令占用4个字节，32比特

type Instruction uint32

func (self Instruction) Opcode() int {
	code := int(self & 0x3F)
	return code // 6 bits
}

func (self Instruction) ABC() (a, b, c int) {
	a = int(self >> 6 & 0xFF)   // 8 bits
	b = int(self >> 14 & 0x1FF) // 9 bits
	c = int(self >> 23 & 0x1FF) // 9 bits
	return
}

func (self Instruction) ABx() (a, bx int) {
	a = int(self >> 6 & 0xFF)
	bx = int(self >> 14)
	return
}

func (self Instruction) AsBx() (a, sbx int) {
	a, bx := self.ABx()
	return a, bx - MAXARG_sBx
}

func (self Instruction) Ax() int {
	return int(self >> 6)
}

func (self Instruction) OpName() string {
	op := opcodes[self.Opcode()]
	return op.name
}

func (self Instruction) OpMode() byte {
	return opcodes[self.Opcode()].opMode
}

func (self Instruction) OpBMode() byte {
	return opcodes[self.Opcode()].argBMode
}

func (self Instruction) OpCMode() byte {
	return opcodes[self.Opcode()].argCMode
}
